
package com.dotools.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.util.Log;

public class ColorUtils {

    /**
     * @param bitmap
     * @param pArr
     * @return
     */
    public static int[] getDomainColor(Bitmap bitmap, ArrayList<Point> pArr) {
        if (bitmap != null && !bitmap.isRecycled()) {
            HashMap colorMap = getColorMap(bitmap, pArr);
            if(!colorMap.isEmpty()) {
                LinkedList list = new LinkedList(colorMap.entrySet());
                Collections.sort(list, new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue());
                    }
                });
                int[] argb = getRGBArr((Integer) (((Map.Entry) list.get(list.size() - 1)).getKey()));
             //   return formatColor(rgb);
                return argb;
            }
        }
        return null;
    }

    private static String formatColor(int[] colorArr) {
        String a = Integer.toHexString(colorArr[0]).length() == 2 ? Integer.toHexString(colorArr[0]) : "0"+Integer.toHexString(colorArr[0]) ;
        String r = Integer.toHexString(colorArr[1]).length() == 2 ? Integer.toHexString(colorArr[1]) : "0"+Integer.toHexString(colorArr[1]) ;
        String g = Integer.toHexString(colorArr[2]).length() == 2 ? Integer.toHexString(colorArr[2]) : "0"+Integer.toHexString(colorArr[2]) ;
        String b = Integer.toHexString(colorArr[3]).length() == 2 ? Integer.toHexString(colorArr[3]) : "0"+Integer.toHexString(colorArr[3]) ;
        return "#"+a+r+g+b;
    }

    private static HashMap getColorMap(Bitmap bitmap, ArrayList<Point> pArr) {
        HashMap m = new HashMap();
        for(int i = 0; i < pArr.size(); i++){
            int rgb = bitmap.getPixel(pArr.get(i).x,pArr.get(i).y);
            int[] rgbArr = getRGBArr(rgb);
      //      if (!isGray(rgbArr)) {
                Integer counter = (Integer) m.get(rgb);
                if (counter == null)
                    counter = 0;
                counter++;
                m.put(rgb, counter);
    //        }
        }
        return m;
    }

    public static int[] getRGBArr(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        return new int[] {
                alpha,red, green, blue
        };
    }

    private static boolean isGray(int[] rgbArr) {
        int rgDiff = rgbArr[0] - rgbArr[1];
        int rbDiff = rgbArr[0] - rgbArr[2];
        int tolerance = 10;
        if (rgDiff > tolerance || rgDiff < -tolerance)
            if (rbDiff > tolerance || rbDiff < -tolerance) {
                return false;
            }
        return true;
    }

}
